package com.sgcc.sx.pmos.mobileapp.dialog;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.listener.OnItemClickListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.mobileapp.adapter.CommunicationAreaDireAdapter;
import com.sgcc.sx.pmos.mobileapp.adapter.CommunicationAreaListAdapter;
import com.sgcc.sx.pmos.mobileapp.bean.CommunicationAreaBean;
import com.sgcc.sx.pmos.mobileapp.bean.ContactInfoBean;
import com.sgcc.sx.pmos.mobileapp.bean.DistrictsInfoBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class CommunicationAreaPopup extends BottomPopupView {

    private RelativeLayout mRlCancle;
    private RelativeLayout mRlDetermine;
    private RecyclerView mRvDirectory;
    private RecyclerView mRvList;

    private Context mContext;
    private CommunicationAreaDireAdapter mDirectoryAdapter;
    private CommunicationAreaListAdapter mListAdapter;
    private List<CommunicationAreaBean> dirList = new ArrayList<>();
    private List<CommunicationAreaBean> list_list = new ArrayList<>();

    public CommunicationAreaPopup(@NonNull Context context, OnCallBackListener onCallBackListener) {
        super(context);
        mContext = context;
        mOnCallBackListener = onCallBackListener;
    }

    public interface OnCallBackListener {
        void save(String values, String lables);
    }

    private OnCallBackListener mOnCallBackListener;


    @Override
    protected int getImplLayoutId() {
        return R.layout.custombottom_communicationareapopup_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mRlCancle = (RelativeLayout) findViewById(R.id.rl_cancle);
        mRlDetermine = (RelativeLayout) findViewById(R.id.rl_determine);
        mRvDirectory = (RecyclerView) findViewById(R.id.rv_directory);
        mRvList = (RecyclerView) findViewById(R.id.rv_list);


        mRlCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mRlDetermine.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer sb_value = new StringBuffer();
                StringBuffer sb_lable = new StringBuffer();
                for (CommunicationAreaBean communicationAreaBean : dirList) {
                    sb_value.append(communicationAreaBean.getValue() + ",");
                    sb_lable.append(communicationAreaBean.getLabel() + ",");
                }
                if (mOnCallBackListener != null) {
                    mOnCallBackListener.save(sb_value.toString(), sb_lable.toString());
                    dismiss();
                }
            }
        });

        LinearLayoutManager mLayoutManagerDirectory = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mRvDirectory.setLayoutManager(mLayoutManagerDirectory);
        mDirectoryAdapter = new CommunicationAreaDireAdapter();
        mDirectoryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object bean, int position) {
                if (position == 0) {
                    dirList.clear();
                    mDirectoryAdapter.setList(dirList);
                    queryDistrictsInfo("CN");

                } else {
                    CommunicationAreaBean communicationAreaBean = (CommunicationAreaBean) bean;
                    dirList = dirList.subList(0, position + 1);
                    mDirectoryAdapter.setList(dirList);
                    queryDistrictsInfo(communicationAreaBean.getValue());
                }
            }
        });
        mRvDirectory.setAdapter(mDirectoryAdapter);

        LinearLayoutManager mLayoutManagerList = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRvList.setLayoutManager(mLayoutManagerList);
        mListAdapter = new CommunicationAreaListAdapter();
        mListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object bean, int position) {
                CommunicationAreaBean communicationAreaBean = (CommunicationAreaBean) bean;
                queryDistrictsInfo(communicationAreaBean.getValue());
                if (!onData2Find(communicationAreaBean)) {
                    dirList.add(communicationAreaBean);
                }
                mDirectoryAdapter.setList(dirList);
            }
        });
        mRvList.setAdapter(mListAdapter);

        defaultContactInfo();
    }

    private boolean onData2Find(CommunicationAreaBean bean) {
        for (CommunicationAreaBean dataBean : dirList) {
            if (dataBean.getValue().equals(bean.getValue())) {
                return true;
            }
        }
        return false;
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }

    private void defaultContactInfo() {
        Map<String, Object> params = new HashMap<>();
        HttpRequestUtils.getInstance().request(params, HttpInterConst.DEFAULTCONTACTINFO, new MvcNetCallback<ContactInfoBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(ContactInfoBean response, int id) {
                super.onResponse(response, id);

                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        if (!response.getData().isEmpty()) {
                            for (ContactInfoBean.DataBean dataBean : response.getData()) {
                                if (dataBean != null) {
                                    if (dataBean.getDistricsParamsBo() != null && !dataBean.getDistricsParamsBo().isEmpty()) {
                                        recursive(dataBean.getDistricsParamsBo());
                                        mDirectoryAdapter.setList(dirList);
                                        if (dirList.size() > 0) {
                                            queryDistrictsInfo(dirList.get(dirList.size() - 1).getValue());
                                            break;
                                        } else {
                                            queryDistrictsInfo("CN");
                                            //当getDistricsParamsBo为null需要传CN重新查数据
                                        }
                                    } else {
                                        queryDistrictsInfo("CN");
                                        //当getDistricsParamsBo为null需要传CN重新查数据
                                    }
                                }
                            }
                        }else {
                            queryDistrictsInfo("CN");
                            //当getDistricsParamsBo为null需要传CN重新查数据
                        }
                    }
                }
            }
        });
    }

    private void recursive(List<CommunicationAreaBean> districsParamsBo) {
        for (CommunicationAreaBean bean : districsParamsBo) {
            if (bean != null) {
                if (bean.getChildren() != null && !bean.getChildren().isEmpty()) {
                    dirList.add(bean);
                    recursive(bean.getChildren());
                } else {
                    dirList.add(bean);
                }
            }
        }
    }


    private void queryDistrictsInfo(String districtPid) {
        Map<String, Object> params = new HashMap<>();
        params.put("level", "");
        params.put("districtPid", districtPid);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERYDISTRICTSINFO, new MvcNetCallback<DistrictsInfoBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(DistrictsInfoBean response, int id) {
                super.onResponse(response, id);

                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        if (null == response.getData() || response.getData().size() == 0) {
                            mRlDetermine.setVisibility(VISIBLE);
                        } else {
                            mRlDetermine.setVisibility(GONE);
                        }
                        list_list.clear();
                        list_list.addAll(response.getData());
                        mListAdapter.setList(list_list);
                    }
                }
            }
        });
    }
}
